package com.fs.console.base.utils;


import com.baomidou.mybatisplus.annotation.TableName;
import com.fs.common.annotation.ColumnDesc;
import com.fs.common.pojo.ModleField;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc 模块数据解析
 * @author TiddlerCJ
 *
 */
@Slf4j
public class ModleTools {

	public static String[] modleClassList(String pn) {
		StringBuffer sBuffer = new StringBuffer();
		try {
			//指定需要检索Annotation的路径，可以使用通配符
			//String pn = "com/zslin/*/controller/*/*Controller.class";
			//1、创建ResourcePatternResolver资源对象
			ResourcePatternResolver rpr = new PathMatchingResourcePatternResolver();
			//2、获取路径中的所有资源对象
			Resource[] ress = rpr.getResources(pn);
			//3、创建MetadataReaderFactory来获取工程
			MetadataReaderFactory fac = new CachingMetadataReaderFactory();
			//4、遍历资源
			for(Resource res:ress) {
				MetadataReader mr = fac.getMetadataReader(res);
				String cname = mr.getClassMetadata().getClassName();
				AnnotationMetadata am = mr.getAnnotationMetadata();
				if(am.hasAnnotation(TableName.class.getName())) {
					sBuffer.append(cname).append(",");
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			e.printStackTrace();
		}
		return sBuffer.toString().split(",");
	}
	/*
	 * 获取实体自定义字段描述
	 */
	public static List<ModleField> modleField(String className){
		List<ModleField> list = new ArrayList<ModleField>();
		try {
			Field[] fds = Class.forName(className).getDeclaredFields();
			ModleField mField = null;
			for(Field field: fds){
				boolean isHave = field.isAnnotationPresent(ColumnDesc.class);
				if(isHave){
					ColumnDesc columnDesc = field.getAnnotation(ColumnDesc.class);
					mField = new ModleField(field.getName(), columnDesc.name());
				}else{
					mField = new ModleField(field.getName(), "名称待定");
				}
				list.add(mField);
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			e.printStackTrace();
		}
		return list ;
	}
	
	/*public static Echo getEchoByClassName(String className){
		Echo echo = null;
		try {
			Annotation[] as = Class.forName(className).getAnnotations();
			for(Annotation a: as){
				if(a instanceof Echo) echo = (Echo) a;
			}
		} catch (Exception e) {}
		return echo ;
	}
	
	public static EasyUiComboBox getEasyUiComboBoxByClassName(String className){
		EasyUiComboBox box = null;
		try {
			Annotation[] as = Class.forName(className).getAnnotations();
			for(Annotation a: as){
				if(a instanceof EasyUiComboBox) box = (EasyUiComboBox) a;
			}
		} catch (Exception e) {}
		return box ;
	}*/
	
	/**
	 * 通过类名查找对应的标注对象
	 * @param className
	 * @param annotationClass
	 * @return
	 */
	public static Annotation getAnnotationByName(String className,Class<?> annotationClass){
		Annotation annotation = null;
		try {
			Class<?> c = null;
			try {
				if(className.indexOf(".")>-1){
					//className = SysConstant.MODLE_CLASSNAME.get(className);
				}
				c = Class.forName(className);
			} catch (Exception e) {
				//e.printStackTrace();
			}
			if(c!=null){
				Annotation[] as = Class.forName(className).getAnnotations();
				if(as!=null && as.length>0){
					for(Annotation a: as){
						if(a.toString().indexOf(annotationClass.getName())>-1) {
							annotation = a;
						}
					}
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			e.printStackTrace();
		}
		return annotation ;
	}
}
